package com.xwd.dao;

import com.xwd.utils.PropertiesUtil;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Properties;

/**
 * @ClassName ConnectionPool
 * @Description: com.com.xwd.utils
 * @Auther: xiwd
 * @Date: 2022/2/7 - 02 - 07 - 19:07
 * @version: 1.0
 */
public class ConnectionPool {
    //properties
    private static String DRIVER;
    private static String URL;
    private static String USERNAME;
    private static String PASSWORD;
    private static int maxSize;
    private static int minSize;
//    private static Properties properties;
    private static LinkedList<Connection> pools;
    private static PropertiesUtil propertiesUtil;

    //block
    static {
        //获取输入流对象
//        InputStream inputStream = ConnectionPool.class.getClassLoader().getResourceAsStream("jdbc.properties");
        propertiesUtil =new PropertiesUtil("jdbc.properties");
        try {
//            properties=new Properties();
//            properties.load(inputStream);
            //初始化Connection数据库连接参数
//            DRIVER=properties.getProperty("driver");
            DRIVER=propertiesUtil.getProperty("driver");
//            URL=properties.getProperty("url");
            URL=propertiesUtil.getProperty("url");
//            USERNAME=properties.getProperty("username");
            USERNAME=propertiesUtil.getProperty("username");
//            PASSWORD=properties.getProperty("password");
            PASSWORD=propertiesUtil.getProperty("password");
//            minSize=Integer.valueOf(properties.getProperty("minSize"));
            minSize=Integer.valueOf(propertiesUtil.getProperty("minSize"));
//            maxSize=Integer.valueOf(properties.getProperty("maxSize"));
            maxSize=Integer.valueOf(propertiesUtil.getProperty("maxSize"));
            //注册数据库连接驱动
            Class.forName(DRIVER);
            //初始化数据库连接池
            pools=new LinkedList<>();
            for (int i = 0; i < minSize; i++) {
                Connection connection = initConnection();
                System.out.println("Connection "+connection.hashCode()+" has been initialized");
                pools.addFirst(connection);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("JDBC Driver Initialized ERROR!");
        }
    }

    //methods
    /*
     * 初始化minSize个数据库连接对象
     * @return
     */
    private static Connection initConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Connection "+connection.hashCode()+" initialized ERROR!");
        }
        return connection;
    }

    /*
     * 从数据库连接池中获取数据库连接对象
     * @return
     */
    public static Connection getConnection(){
        Connection connection=null;
        //判断数据库连接池中是否还有Connection
        if (pools.size()>0){
            connection=pools.removeLast();
        }else {
            connection = initConnection();
        }
        return connection;
    }


    /*
     * 归还数据库连接对象
     * @param connection 要归还的数据库连接对象
     */
    public static void returnConnection(Connection connection){
        if (connection==null){
            System.out.println("OPERATION was denied because connection is null");
            return;
        }
        //如果connection未被关闭
        try {
            if (!connection.isClosed()) {
                //修改事务处理模式为——自动提交模式
                connection.setAutoCommit(true);
                //判断pools是否达到存储上限
                if (pools.size()<maxSize){
                    //归还数据库连接对象
                    pools.addFirst(connection);
                }else {
                    connection.close();
                }
            }else {
                System.out.println("Connection "+connection.hashCode()+" has been closed already!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }



}
